package zxing.activity;

import android.content.Intent;
import android.os.Binder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import org.litepal.crud.DataSupport;

import java.sql.Date;
import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.virtualapp.R;
import io.virtualapp.abs.ui.VActivity;
import io.virtualapp.bean.MakeBean;
import io.virtualapp.bean.NumBean;

public class WifiCodeActivity extends VActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.radioGroup_gender)
    RadioGroup radioGroup_gender;
    @BindView(R.id.radio_wap)
    RadioButton radio_wap;
    @BindView(R.id.radio_web)
    RadioButton radio_web;
    @BindView(R.id.radio_no)
    RadioButton radio_no;
    @BindView(R.id.head_back)
    ImageView head_back;
    @BindView(R.id.head_center_title)
    TextView head_center_title;
    @BindView(R.id.edit_name)
    EditText edit_name;
    @BindView(R.id.edit_password)
    EditText edit_password;
    @BindView(R.id.but)
    Button but;
    String tab;
    //WIFI:T:（加密方式，一般WPA或者WPA2）;S:（无线名称）;P:（无线连接密码）;;
    //WIFI:S:TestWiFi;T:WPA;P:123456;;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi_code);
        ButterKnife.bind(this);
        radioGroup_gender.setOnCheckedChangeListener(this);
        radio_wap.setChecked(true);
        tab = radio_wap.getText().toString();
        initView();
    }

    public void initView() {

        head_center_title.setText("WIFI二维码");
        head_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(edit_name.getText().toString())) {
                    Toast.makeText(WifiCodeActivity.this, "WIFI名称不能为空！", Toast.LENGTH_SHORT).show();
                    return;
                }
                String contents = "WIFI:"
                        + "S:" + edit_name.getText().toString() + ";" + "T:" + tab + ";" + "P:" + edit_password.getText().toString()
                        + ";;";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
                Date date = new Date(System.currentTimeMillis());
                String n = simpleDateFormat.format(date);
                MakeBean user = new MakeBean();
                user.setName(n);
                user.setTit(contents + "");
                user.save();
                NumBean numBean = new NumBean();
                if (DataSupport.findFirst(NumBean.class) != null) {
                    DataSupport.deleteAll(NumBean.class);
                }
                numBean.setNum(2);
                numBean.save();
                Intent intent = new Intent(getActivity(), CodeDisplayActivity.class);
                intent.putExtra("data", contents + "");
                intent.putExtra("title", "WIFI二维码");
                startActivity(intent);
            }
        });
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == radio_wap.getId()) {
            tab = radio_wap.getText().toString();
        } else if (checkedId == radio_web.getId()) {
            tab = radio_web.getText().toString();
        } else if (checkedId == radio_no.getId()) {
            tab = "";
        }
    }
}
